package com.wgh.product.converter;

import com.wgh.product.dto.ProductReviewDTO;
import com.wgh.product.entity.shop.ProductReview;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.time.ZoneId;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 商品评价转换器
 * 用于ProductReview实体和ProductReviewDTO之间的转换
 */
@Component
public class ProductReviewConverter {

    /**
     * 将ProductReview实体转换为ProductReviewDTO
     * @param productReview 商品评价实体
     * @return 商品评价DTO
     */
    public ProductReviewDTO toDTO(ProductReview productReview) {
        if (productReview == null) {
            return null;
        }
        
        ProductReviewDTO productReviewDTO = new ProductReviewDTO();
        productReviewDTO.setId(productReview.getId());
        productReviewDTO.setProductId(productReview.getProductId());
        productReviewDTO.setSkuId(productReview.getSkuId());
        productReviewDTO.setUserId(productReview.getUserId());
        productReviewDTO.setOrderId(productReview.getOrderId());
        productReviewDTO.setScore(productReview.getScore());
        productReviewDTO.setContent(productReview.getContent());
        productReviewDTO.setImages(productReview.getImages());
        productReviewDTO.setVideo(productReview.getVideo());
        productReviewDTO.setHasImage(productReview.getHasImage());
        productReviewDTO.setHasVideo(productReview.getHasVideo());
        productReviewDTO.setStatus(productReview.getStatus());
        productReviewDTO.setReplyContent(productReview.getReplyContent());
        productReviewDTO.setReplyTime(productReview.getReplyTime());
        productReviewDTO.setIsAnonymous(productReview.getIsAnonymous());
        productReviewDTO.setCreateTime(Date.from(productReview.getCreateTime().atZone(ZoneId.systemDefault()).toInstant()));
        productReviewDTO.setUpdateTime(Date.from(productReview.getUpdateTime().atZone(ZoneId.systemDefault()).toInstant()));
        productReviewDTO.setImageList(productReview.getImageList());
        
        return productReviewDTO;
    }

    /**
     * 将ProductReviewDTO转换为ProductReview实体
     * @param productReviewDTO 商品评价DTO
     * @return 商品评价实体
     */
    public ProductReview toEntity(ProductReviewDTO productReviewDTO) {
        if (productReviewDTO == null) {
            return null;
        }
        
        ProductReview productReview = new ProductReview();
        productReview.setId(productReviewDTO.getId());
        productReview.setProductId(productReviewDTO.getProductId());
        productReview.setSkuId(productReviewDTO.getSkuId());
        productReview.setUserId(productReviewDTO.getUserId());
        productReview.setOrderId(productReviewDTO.getOrderId());
        productReview.setScore(productReviewDTO.getScore());
        productReview.setContent(productReviewDTO.getContent());
        productReview.setImages(productReviewDTO.getImages());
        productReview.setVideo(productReviewDTO.getVideo());
        productReview.setHasImage(productReviewDTO.getHasImage());
        productReview.setHasVideo(productReviewDTO.getHasVideo());
        productReview.setStatus(productReviewDTO.getStatus());
        productReview.setReplyContent(productReviewDTO.getReplyContent());
        productReview.setReplyTime(productReviewDTO.getReplyTime());
        productReview.setIsAnonymous(productReviewDTO.getIsAnonymous());
        productReview.setCreateTime(productReviewDTO.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        productReview.setUpdateTime(productReviewDTO.getUpdateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        productReview.setImageList(productReviewDTO.getImageList());
        
        return productReview;
    }

    /**
     * 将ProductReview实体列表转换为ProductReviewDTO列表
     * @param productReviews 商品评价实体列表
     * @return 商品评价DTO列表
     */
    public List<ProductReviewDTO> toDTOList(List<ProductReview> productReviews) {
        if (productReviews == null || productReviews.isEmpty()) {
            return new ArrayList<>();
        }
        
        List<ProductReviewDTO> productReviewDTOs = new ArrayList<>();
        for (ProductReview productReview : productReviews) {
            productReviewDTOs.add(toDTO(productReview));
        }
        
        return productReviewDTOs;
    }

    /**
     * 将ProductReviewDTO列表转换为ProductReview实体列表
     * @param productReviewDTOs 商品评价DTO列表
     * @return 商品评价实体列表
     */
    public List<ProductReview> toEntityList(List<ProductReviewDTO> productReviewDTOs) {
        if (productReviewDTOs == null || productReviewDTOs.isEmpty()) {
            return new ArrayList<>();
        }
        
        List<ProductReview> productReviews = new ArrayList<>();
        for (ProductReviewDTO productReviewDTO : productReviewDTOs) {
            productReviews.add(toEntity(productReviewDTO));
        }
        
        return productReviews;
    }
}